Ext.define('cp.view.form.TurnoBase',{
   extend: 'Ext.form.Panel',

   
    bodyPadding: '5px 5px 5px 5px', 
    bodyStyle: 'border: none',
    fieldDefaults: {
        labelWidth: 65,
        labelAlign: 'top'
    },
   
   initComponent: function(){
       
       var diaMin = new Date();
       var diaMax = Ext.Date.add(new Date(), Ext.Date.DAY, +6);
       
       var dsPaciente = Ext.create('cp.store.form.PacienteActivo',{});
       var dsObras = Ext.create('cp.store.form.ObraActiva',{});
       var dsHoras = Ext.create('cp.store.form.HorasMedico',{});
       
       this.action = this.actionUrl;
       
       var info = [
            { fieldLabel: 'Paciente', name: 'paciente', allowBlank: false, 
              xtype: 'combobox', valueField:'idUsuario', displayField:'nombre',
              store: dsPaciente
            },
            { fieldLabel: 'Obra social', name: 'obra', allowBlank: false, 
              xtype: 'combobox', valueField:'idObra', displayField:'obra',
              store: dsObras
            },
            { fieldLabel: 'N° afiliado', name: 'nroAfiliado', allowBlank: true, 
              xtype: 'textfield', vtype: 'identificacion', minLength: 5,
              disabled: true
            },
            { fieldLabel:  'Fecha estimada', name: 'fecha', xtype: 'datefield',
              allowBlank: false, format: 'Y-m-d', minValue: diaMin,
              maxValue: diaMax
            },
            { fieldLabel: 'Médico', name: 'medico', allowBlank: false, 
              xtype: 'combobox', valueField: 'idUsuario', displayField: 'nombre',
              disabled: true
            },
            { fieldLabel:  'Hora estimada', name: 'hora', xtype: 'combobox',
              allowBlank: false, disabled: true, displayField: 'hora', valueField: 'hora',
              queryMode: 'local', store: dsHoras
            }
        ];
        
        
        this.items = [
            {
                bodyStyle: 'border: none',
                layout: 'column',
                itemId: 'columnas',
                items: [
                    {
                        xtype: 'fieldset', defaultType: 'textfield', title: 'Turno', 
                        layout: 'column', items:
                        [
                            {xtype: 'fieldset', defaultType: 'textfield', style: 'border: none',
                                items: info}
                        ]
                    }
              ]
            }
         ];  
        
       this.callParent(arguments);
    },
    submitForm: function(){
        if(this.getForm().isValid()){
            this.sendData();
        }else{
            Ext.create('cp.view.ui.AlertErr',{
                mensaje: 'El formulario contiene errores. Por favor, corrigalos, y vuelva a realizar la operación.'
                    }).showDialog();    
        }
    },
    sendData: function(){
        var me = this;
        this.getForm().submit({
            url: me.action,
            method: 'POST',
            success: function(form,action){
                Ext.create('cp.view.ui.AlertOk',{mensaje:action.result.msg}).showDialog();
                me.up('decoratorForm').close();
                Ext.ComponentQuery.query('gestionTurno')[0].getStore().load();                 
            },
            failure: function(form, action){
                Ext.create('cp.view.ui.AlertErr',{mensaje:action.result.msg}).showDialog();
            }
        });
    }
});